#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    QMenuBar* menubar = new QMenuBar();
    QMenuBar* menubar = this->menuBar();
    this->setMenuBar(menubar);

    // 构建菜单
//    QMenu* menu1 = new QMenu("文件");
//    QMenu* menu2 = new QMenu("编辑");

    // 快捷键
    QMenu* menu1 = new QMenu("文件(&N)");
    QMenu* menu2 = new QMenu("编辑(&E)");

//    menu1->addAction("新建");
//    menu1->addAction("打开");
//    menu1->addAction("保存");
//    menu1->addAction("另存为");
    QAction* action1 = new QAction("新建");
    QAction* action2 = new QAction("打开");
    QAction* action3 = new QAction("保存");
    QAction* action4 = new QAction("另存为");
    menu1->addAction(action1);
    menu1->addAction(action2);
    menu1->addAction(action3);
    menu1->addAction(action4);

    // 设置槽函数
    connect(action1,&QAction::triggered, this, &MainWindow::handle1);
    connect(action2,&QAction::triggered, this, &MainWindow::handle2);
    connect(action3,&QAction::triggered, this, &MainWindow::handle3);
    connect(action4,&QAction::triggered, this, &MainWindow::handle4);

    // 添加菜单
    menubar->addMenu(menu1);
    menubar->addMenu(menu2);

    // 添加子菜单
    QMenu* child = new QMenu("子菜单");
    child->addAction("选项一");
    child->addAction("选项二");
    child->addAction("选项三");
    menu2->addMenu(child);

    // 图标设置
//    menu1->setIcon(QIcon(":/1.jpg"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handle1()
{
    qDebug() << "文件 - 新建 ";
}
void MainWindow::handle2()
{
    qDebug() << "文件 - 打开";
}
void MainWindow::handle3()
{
    qDebug() << "文件 - 保存";
}
void MainWindow::handle4()
{
    qDebug() << "文件 - 另存为";
}
